layui.use(['form', 'table','layuimini','element', 'laydate'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini,
        laydate = layui.laydate;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var BoFile = {
        id: 'BoFileTable'
    };

    var column = [
        [
            {type: 'numbers'},
            {field: 'serviceId', title: '业务编号', align: 'center'},
            {field: 'articleTitle', title: '文章标题', align: 'center'},
            {field: 'fileName', title: '附件名', align: 'center'},
            {field: 'fileOrgname', title: '附件原名', align: 'center'},
            {field: 'fileType', title: '后缀名', align: 'center'},
            {field: 'fileSize', title: '附件大小', align: 'center',
                templet: function (object) {
                    var size = object.fileSize / 1024 / 1024;
                    return size.toFixed(4) + 'M';
                }
            },
            {field: 'calssity', title: '分类', align: 'center',
                templet: function (object) {
                    if(0 == object.calssity){
                        return '<span class="layui-badge layui-bg-orange">文章</span>';
                    }
                }
            },
            {field: 'createDate', title: '上传时间', minWidth: 120, align: 'center'},
            {templet: '#stateBtn', width: 140, align: 'center', title: '操作'}
        ]
    ];

    table.render({
        elem: '#' + BoFile.id,
        url: '/web/boFile/list',
        toolbar: '#toolbarBtn',
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    // 监听搜索操作
    $("#data-search-btn").on('click', function () {

        //获取搜索条件
        var serviceId = $("#search_serviceId").val();
        var fileType = $("#search_fileType").val();
        var calssity = $("#search_calssity").val();
        var articleTitle = $("#search_articleTitle").val();

        //封装
        var dataObject = {};
        dataObject['serviceId'] = serviceId;
        dataObject['fileType'] = fileType;
        dataObject['calssity'] = calssity;
        dataObject['articleTitle'] = articleTitle;
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoFile.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    // 监听重置操作
    $("#data-reset-btn").on('click', function () {

        //初始化
        $("#search_serviceId").val("");
        $("#search_fileType").val("");
        $("#search_calssity").val("-1");
        $("#search_articleTitle").val("");

        form.render();

        //封装
        var dataObject = {};
        dataObject['serviceId'] = "";
        dataObject['fileType'] = "";
        dataObject['calssity'] = "-1";
        dataObject['articleTitle'] = "";
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoFile.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    /**
     * 点击删除按钮
     * @param id
     */
    BoFile.delete = function(id){
        layer.open({
            title: '删除附件'
            ,content: '是否确定删除该附件？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/web/boFile/delete';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(BoFile.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };


    //监听表格复选框选择
    // table.on('checkbox(currentTableFilter)', function (obj) {
    //     console.log(obj)
    // });

    //监听工具条
    table.on('tool(' + BoFile.id + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'delete') {
            BoFile.delete(data.id);
        }
    });

});